﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;

public partial class Templates_Panoramic_Default : Acms.AcmsControls.TemplatePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsSibling.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsSibling_SelectData);
        dlNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavSiblings_ItemDataBound);
        //post = AcmsContext.CurrentPost();
        //var post = e.Page as Post;
        //var HeroImage = post.FindPostControl("HeroImage");// as ImageContentControlDisplay;
        //if (HeroImage != null)
        //{
        //    var prop = HeroImage.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
        //    if (prop != null && prop.HasResource())
        //    {
        //        ImgSrc = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
        //    }
        //}

 
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }


    public override void DataBind()
    {
        base.DataBind();
        var post = AcmsContext.CurrentPost;

        if (post != null)
        {
            //var pnlItem = pnlItem;
            if (pnlItem != null)
            {
                var HeroImage = post.FindPostControl("HeroImage");// as ImageContentControlDisplay;
                if (HeroImage != null)
                {
                    var prop = HeroImage.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                    if (prop != null && prop.HasResource())
                    {
                        pnlItem.Width = prop.Width;
                        pnlItem2.Width = prop.Width;
                        //pnlItem.BackImageUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
                    }
                }
            }
            

            var tabs = post.Children.Where(x => x.TemplateName == "TabHolder").ToArray();


            //var moreInfo = this.FindControl("moreInfo") as HyperLink;

            if (lnkMoreInfo != null)
            {
                if (tabs.Length > 0)
                {
                    lnkMoreInfo.Visible = true;
                }
                else
                {
                    lnkMoreInfo.Visible = false;
                }

            }
            rptTabs.DataSource = tabs;
            rptTabs.DataBind();
        }
    } 


    //protected string GetFlash()
    //{
    //    return "http://" + HttpContext.Current.Request.Url.Host + ResolveUrl("~/Flash/Panoramic.swf");
    //}
    
    //protected string GetImgSrc()
    //{
    //    var heroImage = AcmsContext.CurrentPost.FindPostControl("HeroImage");
    //    var host = HttpContext.Current.Request.Url.Host;
    //    if (heroImage != null)
    //    {
    //        var heroImageProperties = heroImage.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
    //        if (heroImageProperties != null)
    //        {
    //            return "http://"+host+ResolveUrl(Acms.AcmsControls.RenderFile.GetUrl(heroImageProperties.ResourceId, null, null, this));
    //        }
    //    }
    //    return null;
    //}
    
    void dsSibling_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }
    void dsTabChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Children.Where(x => x.TemplateName == "TabItem").OrderBy(x => x.Sequence).ToList());
    }
    void dlNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNavSibling") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }

}
